package com.singzi.leetcode.jianzhiOffer;

import java.util.List;
import java.util.Objects;
import java.util.Stack;

public class T0030 {
    public static void main(String[] args) {
        MinStack minStack = new MinStack();
        minStack.push(512);
        minStack.push(-1024);
        minStack.push(-1024);
        minStack.push(512);
        minStack.pop();
        System.out.println(minStack.min());
        minStack.pop();
        System.out.println(minStack.min());
        minStack.pop();
        System.out.println(minStack.min());
    }
}
class MinStack {

    private Integer minNumber = Integer.MAX_VALUE;
    Stack<Integer> stack;

    /** initialize your data structure here. */
    public MinStack() {
        stack = new Stack<>();
    }

    public void push(int x) {
        if(x < minNumber) minNumber = x;
        stack.push(x);
    }

    public void pop() {
        if(Objects.equals(stack.peek(), minNumber)){
            stack.pop();
            if(!stack.isEmpty()){
                minNumber = stack.peek();
                for(Integer integer:stack){
                    if(integer < minNumber){
                        minNumber = integer;
                    }
                }
            }
            else minNumber = Integer.MAX_VALUE;
            return;
        }
        stack.pop();
    }

    public int top() {
        return stack.peek();
    }

    public int min() {
        return minNumber;
    }
}

/**
 * Your MinStack object will be instantiated and called as such:
 * MinStack obj = new MinStack();
 * obj.push(x);
 * obj.pop();
 * int param_3 = obj.top();
 * int param_4 = obj.min();
 */